﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

/**
 * shikl
 * 2012-04-13
 * 对数据库操作的封装类
 */

namespace SqlDalLibrary
{
    public class DbManager
    {
        protected static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
        //构造函数
        public DbManager()
        {
        }
        //执行SQL语句,返回影响记录数
        public static int ExecuteSql(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
        }
        //执行SQL语句,返回DataSet
        public static DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }
    }
}